package com.hevs.samplewebapp.client.services;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.JsArray;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.hevs.samplewebapp.client.Patient;
import com.hevs.samplewebapp.client.bean.JSONHelper;
import com.hevs.samplewebapp.client.bean.MedicalDataItem;
import com.hevs.samplewebapp.client.bean.MedicalDataJson;
import com.hevs.samplewebapp.client.dialogbox.ServerResponseMessages;
import com.hevs.samplewebapp.client.screens.StatisticsWidget;

/**
 * Implementation of the statistics service Interface.
 * 
 * @author Thomas Rouvinez
 * @Date: 23.04.2012
 *
 */
public class StatisticsDataServiceImpl implements StatisticsDataService{
	// Variables.
	/**
	 * List of medical data
	 */
	private List<MedicalDataItem> data =  new ArrayList<MedicalDataItem>();
	
	/**
	 * Array of medical data
	 */
	private MedicalDataItem[] useableData = null;
	
	@Override
	/**
	 * Method to retrieve the medical data from the server
	 */
	public void getFullMedicalData() {
		String url = Patient.JSON_URL_MEDICALDATA;
		url = URL.encode(url);
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
		
		try {
			@SuppressWarnings("unused")
			Request request = builder.sendRequest(null, new RequestCallback() {
				
				public void onError(Request request, Throwable exception) {
					Window.alert("Couldn't retrieve JSON");
				}

				public void onResponseReceived(Request request, Response response) {
					if(200 == response.getStatusCode()){
						// Get new list of medical data.
						convertJSToList(JSONHelper.asArrayOfMedicalData(response.getText()));
						StatisticsWidget.statistics.setMedicalDataInput(getData());
						StatisticsWidget.statistics.setDataProcessed(true);
					}else
						ServerResponseMessages.statusCode(response.getStatusCode());
				}
			});
		} catch (Exception e) {
			Window.alert("Error while retrieving medical data!");
		}
	}

	/**
	 * Getter for the array of medical data
	 * @return
	 * an array of medical data
	 */
	public MedicalDataItem[] getData(){
		return this.useableData;
	}
	
	/**
	 * Method to convert a medicalDataItem from JSON to Java object.
	 * @param array
	 * the JSON array to convert from the server
	 */
	private void convertJSToList(JsArray<MedicalDataJson> array){
		data = new ArrayList<MedicalDataItem>();
		
		for(int i = 0 ; i < array.length(); i++){
			MedicalDataItem item = new MedicalDataItem();
			
			item.setDate(array.get(i).getDate());
			item.setType(array.get(i).getType());
			item.setCategory(array.get(i).getCategory());
			item.setValue(array.get(i).getValue());
			
			data.add(item);
		}
		
		// Convert list to array.
		useableData = new MedicalDataItem[data.size()];
		
		for(int i = 0 ; i < useableData.length ; i++){
			useableData[i] = data.get(i);
		}
	}
}
